<?php

require_once './include/common.inc.php';
checkLogin();
$page_title = 'jans2en furniture - Inventory - Inventory Listing';
$menu_id = 6;
$page_guide_name2 = '<a href="inventory.php">Inventory</a>';

getMyParam('inventoryid');
getMyParam('categoryid');
getMyParam('fid');
getMyParam('special');
setStatus($special);
$fid = (int)$fid;

if (!is_numeric($inventoryid) || $inventoryid <=0) {
    header("Location:inventory.php");
}

if ($jansen_type==2) {
    $sql_where = ' WHERE 1=1';
} elseif ($jansen_type == 1) {
    $sql_where = ' WHERE n.`type` = 1 ';
    //$inventoryid = 1;
}

/*
 * 
 * 1. ȡǰûܹinventoryб,߲˵ʾ
 * 2. òƷΪɾʱsql
 * 3. ȡǰinventoryлƷ>0ķ࣬seq>0,qty>0(special=1 => special_price>0)
 * 4. ѯ÷µвƷϢ
 */

$sql = "SELECT `update` FROM `inventory` WHERE `id`=$inventoryid";
//echo $sql;
$tmp_ary = $db->get_one($sql);
$update_time = $tmp_ary['update'] == 0 ? '01/05/2006' : date('d/M/Y', $rs['update']);

// =============== 1 start =============================
//$sql = "SELECT `id`, `name`, `pic`, `type` FROM `inventory` " . $sql_where . " ORDER BY `seq` ASC";
$sql = "SELECT n.`id`, n.`name`, n.`pic`, n.`type`, n.`update` FROM `inventory` as n, `product_inventory` as i " . $sql_where . $sql_2 . $sql_3 . " GROUP BY n.`id` ORDER BY n.`seq` ASC";

$query = $db->query($sql);
while($rs = $db->fetch_array($query)) {
    $inventory_show_ary[] = $rs;
    if ($inventoryid == $rs['id']) {
        $inventory_type = $rs['type'];
        $update_time = $rs['update'] == 0 ? '01/Jan/2006' : date('d/M/Y', $rs['update']);
    }
}
// =============== 1 end ===============================

$cate_id = $fid>0 ? $fid : $categoryid;


if (!is_numeric($categoryid) || $categoryid <=0) {
    header("Location:inventory.php");
}

// =============== 2 start =============================
//delete_flag=1ĲƷʾ
$delete_flag_str = '';

$sql = "SELECT `id` FROM `product` WHERE `delete_flag` = 1";
$query = $db->query($sql);
while($rs = $db->fetch_array($query)) {
    $delete_flag_ary[] = $rs['id'];   //ΪɾĲƷ
}

if (!empty($delete_flag_ary)) {
    $delete_flag_str = " AND c.`product_id` NOT IN (" . implode(",", $delete_flag_ary) . ") ";
}
// =============== 2 end ===============================

// =============== 3 start =============================
//$sql = "SELECT c.`category_id`, c.`product_id`, i.`qty` FROM `product_inventory` i, `product_category` c WHERE i.`product_id`=c.`product_id` AND c.`seq2`>0 AND i.`qty`>0 AND i.`inventory_id` = '$inventoryid' " . $delete_flag_str . "ORDER BY c.`seq2` ASC";
$sql = "SELECT c.`category_id`, c.`product_id`, i.`qty` FROM `product_inventory` i, `product_category` c, `category` ca WHERE i.`product_id`=c.`product_id` AND c.`category_id`=ca.id AND i.`qty`>0 ".$sql_2 . " AND i.`inventory_id` = '$inventoryid' " . $delete_flag_str . "ORDER BY c.`seq` ASC";

$query = $db->query($sql);
while($rs = $db->fetch_array($query)) {
    $category_id_ary[] = $rs['category_id'];   //вƷķ
    $have_qty_product_ary[] = $rs['product_id'];
    $product_qty_ary[$rs['product_id']] = $rs['qty'];
}

$have_qty_product_ary = array_unique($have_qty_product_ary);
$category_id_ary = array_unique($category_id_ary);
$category_id_str = implode(",", $category_id_ary);

$sql = "SELECT `id`, `name`, `pic`, `pic_2`, `seq`, `fid`, `description` FROM `category` c WHERE not exists(select id from category where id=c.fid and publish = 0) AND  c.`fid`>0 AND c.publish =1 AND c.`id` IN (" . $category_id_str . ")";
$query = $db->query($sql);
$sub_category_ary[] = $categoryid;
while($rs = $db->fetch_array($query)) {
    $category_id_ary[] = $rs['fid'];
    if ($rs['fid'] == $categoryid) {
        $sub_category_ary[] = $rs['id'];
    }
    
}

$category_id_ary = array_unique($category_id_ary);
$have_set_category_id_str = implode(",", $category_id_ary);

$categoryid_str = $categoryid;
$sql = "SELECT `id`, `fid`, `name`, `pic`, `pic_2`, `seq`, `description` FROM `category` c WHERE not exists(select id from category where id=c.fid and publish = 0) AND  `id` IN (" . $have_set_category_id_str . ") AND publish=1 AND `fid` IN (0,$cate_id) ORDER BY `seq` ASC";

$query = $db->query($sql);
while($rs = $db->fetch_array($query)) {
    if ($rs['fid'] == 0) {
        $out_ary[] = $rs;
        if ($categoryid == $rs['id']) {
            $category_name = $rs['name'];
            $category_pic_2 = $rs['pic_2'];
        }
    } else {
        $sub_out_ary[] = $rs;
        $categoryid_str .= ',' . $rs['id'];
        if ($categoryid == $rs['id']) {
            $category_name = $rs['name'];
            $category_pic_2 = $rs['pic_2'];
        }
    }
}


// =============== 3 end ===============================

if ($fid>0) {
    $categoryid_str = $categoryid;
}

// =============== 3 start =============================
// *** page code start ***
$page_url = 'inventory_list.php?inventoryid=' . $inventoryid . '&categoryid=' . $categoryid . '&';
if ($special) {
    $page_url = $page_url . 'special=1&';
}

//$sql_all_num = count($have_qty_product_ary);
$have_qty_product_str = implode(",", $have_qty_product_ary);

$product_ary = array();
$tmp_cate_str = implode(",", $sub_category_ary);

$sql = "SELECT p.`id`, p.`code`, p.`name`, p.`pic`, p.`small_pic`, p.`price_1`, p.`price_2`, p.`price_3` FROM `product` p, `product_category` c, `category` ca WHERE   not exists(select id from category where id=ca.fid and publish = 0) AND ca.`id`=c.`category_id` AND p.`id`=c.`product_id` AND c.`category_id` IN (" . $tmp_cate_str . ") AND p.`id` IN (" . $have_qty_product_str . ") GROUP BY p.`id`";
$query = $db->query($sql);
$sql_all_num = $db->num_rows($query);

$ppp = 12;
getMyParam('page');
$page = empty($page) || !ispage($page) ? 1 : $page;
$start_limit = ($page - 1) * $ppp;
if($start_limit > $sql_all_num) {
    $start_limit = 0;
    $page = 1;
}

$page_show = getPhotoPageStr($sql_all_num, $page, $ppp, $page_url);
// *** page code end ***

$sql = "SELECT p.`id`, p.`code`, p.`name`, p.`pic`, p.`small_pic`, p.`price_1`, p.`price_2`, p.`price_3`, p.`new_flag` FROM `product` p, `product_category` c, `category` ca WHERE not exists(select id from category where id=ca.fid and publish = 0) AND ca.`id`=c.`category_id` AND p.`id`=c.`product_id` AND c.`category_id` IN (" . $tmp_cate_str . ") AND p.`id` IN (" . $have_qty_product_str . ") GROUP BY p.`id` ORDER BY ca.`fid` asc, ca.`seq` asc, p.`code` DESC, p.`id` DESC LIMIT $start_limit, $ppp";
$query = $db->query($sql);
while($rs = $db->fetch_array($query)) {
	$rs['code'] = strtoupper($rs['code']);
    $product_ary[] = $rs;
    $js_id_ary[] = "'" . $rs['id'] . "'";
    $sp_id_ary[] = $rs['id'];
}

if (!empty($sp_id_ary)) {
    $sp_id_str = implode(",", $sp_id_ary);
    $sql = "SELECT `product_id`, `qty`, `special_price`, `special_price2` FROM `product_inventory` WHERE `product_id` IN (" . $sp_id_str . ") AND `inventory_id`='$inventoryid'";
    $query = $db->query($sql);
    while($rs = $db->fetch_array($query)) {
        $sp_ary[$rs['product_id']] = $rs['special_price'];
        $sp_ary2[$rs['product_id']] = $rs['special_price2'];
    }
}
// =============== 4 end ===============================


$js_id_ary_str = !empty($js_id_ary) ? "new Array(" . implode(',', $js_id_ary) . ")" : "new Array()";

// ͼƬ
if (trim($category_pic_2) == "" || !file_exists("./attachments/" . $category_pic_2)) {
    $category_pic_2 = 'guide_inventory_default.jpg';
}

//echo "<pre>";
//print_r($db->sql_ary);
 
if($inventoryid == '3'){
 
	browseTracking(15,0,'Inventory > HongKong Warehouse > '.$category_name.'(page '.$page.')');
	
}
$postiondiv='<div class="position" id="position"><a href="index.php">Home</a> &gt; <a href="products.php?categoryid=1001"> Inventory</a> </div>';
$baner_img='<div class="bigtitle">Inventory</div>';


require_once PrintEot('header');
require_once PrintEot('inventory_list');
require_once PrintEot('footer');

?>